package menus;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import connectors.Client;
import connectors.Server;
import controller.Controller;
import controller.Controller.GameState;

@SuppressWarnings("serial")
public class MainMenu extends JFrame {

	private JPanel contentPane;
	private JTextField ipField;
	private JTextField portField;
	private Controller controller;

	/**
	 * Create the frame.
	 */
	public MainMenu(Controller control) {
		setTitle("Coffee Wars");
		setResizable(false);
		this.controller = control;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(1, 0, 0, 0));

		JPanel panel = new JPanel();
		contentPane.add(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
				1.0, 0.0, 1.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblNewLabel_1 = new JLabel("Welcome to Coffee Wars!");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.gridwidth = 4;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 3;
		gbc_lblNewLabel_1.gridy = 0;
		panel.add(lblNewLabel_1, gbc_lblNewLabel_1);

		JLabel lblConnectionIp = new JLabel("Connection IP : Port");
		GridBagConstraints gbc_lblConnectionIp = new GridBagConstraints();
		gbc_lblConnectionIp.anchor = GridBagConstraints.WEST;
		gbc_lblConnectionIp.insets = new Insets(0, 0, 5, 5);
		gbc_lblConnectionIp.gridx = 3;
		gbc_lblConnectionIp.gridy = 1;
		panel.add(lblConnectionIp, gbc_lblConnectionIp);

		ipField = new JTextField();
		ipField.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_ipField = new GridBagConstraints();
		gbc_ipField.fill = GridBagConstraints.HORIZONTAL;
		gbc_ipField.insets = new Insets(0, 0, 5, 5);
		gbc_ipField.gridx = 5;
		gbc_ipField.gridy = 1;
		panel.add(ipField, gbc_ipField);
		ipField.setColumns(10);

		JLabel label_1 = new JLabel(":");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 6;
		gbc_label_1.gridy = 1;
		panel.add(label_1, gbc_label_1);

		portField = new JTextField();
		portField.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_portField = new GridBagConstraints();
		gbc_portField.insets = new Insets(0, 0, 5, 5);
		gbc_portField.fill = GridBagConstraints.HORIZONTAL;
		gbc_portField.gridx = 7;
		gbc_portField.gridy = 1;
		panel.add(portField, gbc_portField);
		portField.setColumns(5);

		JLabel lblNewLabel = new JLabel("Hosting: only port required");
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel.gridwidth = 2;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 3;
		gbc_lblNewLabel.gridy = 2;
		panel.add(lblNewLabel, gbc_lblNewLabel);

		JSplitPane splitPane = new JSplitPane();
		GridBagConstraints gbc_splitPane = new GridBagConstraints();
		gbc_splitPane.anchor = GridBagConstraints.WEST;
		gbc_splitPane.gridwidth = 3;
		gbc_splitPane.insets = new Insets(0, 0, 5, 5);
		gbc_splitPane.fill = GridBagConstraints.VERTICAL;
		gbc_splitPane.gridx = 3;
		gbc_splitPane.gridy = 3;
		panel.add(splitPane, gbc_splitPane);

		JButton btnHost = new JButton("Host");
		btnHost.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				String portInput = portField.getText();
				if(portInput.length() == 0) // Input validation
					return;
				
				controller.conn = new Server(Integer.parseInt(portInput));
				controller.state = GameState.MAP_SELECTION;
				
				System.out.println("Connected.");
				
			}
		});
		splitPane.setLeftComponent(btnHost);

		JButton btnJoin = new JButton("Join");
		btnJoin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				String ipInput = ipField.getText();
				String portInput = portField.getText();
				if(ipInput.length() == 0 || portInput.length() == 0) // Input validation
					return;
				
				controller.conn = new Client(ipInput, Integer.parseInt(portInput));
				// TODO handle timeout
				controller.state = GameState.MAP_SELECTION;
				
				System.out.println("Connected.");
				
			}
		});
		splitPane.setRightComponent(btnJoin);
	}

	public void destroy(){
		this.dispose();
	}
}